/*
 * QCalParseEventDetailsXML.cpp
 *
 *  Created on: Aug 19, 2010
 *      Author: deroy2
 */

#include "QCalParseEventDetailsXML.h"

QCalParseEventDetailsXML::QCalParseEventDetailsXML()
{
	//file = 0;

}

 
QCalParseEventDetailsXML::~QCalParseEventDetailsXML()
{
//	if (file)
//		delete file;
}

void QCalParseEventDetailsXML::parseXML(QFile& aFile)
{
	
	QXmlStreamReader xml(&aFile);
	/* We'll parse the XML until we reach end of it.*/
	  while(!xml.atEnd() && !xml.hasError()) 
	{
	        /* Read next element.*/
	        QXmlStreamReader::TokenType token = xml.readNext();
	        /* If token is just StartDocument, we'll go to next.*/
	        if(token == QXmlStreamReader::StartDocument) {
	            continue;
	        }
	        /* If token is StartElement, we'll see if we can read it.*/
	        if(token == QXmlStreamReader::StartElement) {

	            /* If it's named title, we'll dig the information from there.*/
	            if(xml.name() == "entry") {
	            
					getEventInfo(xml);
	            }
	        }
	}
	    /* Error handling. */
	if(xml.hasError()) 
	{
	       QMessageBox::critical(0,
	                            "ParseEventXML::parseXML",
	                              xml.errorString(),
	                              QMessageBox::Ok);
	}
	/* Removes any device() or data from the reader
	 * and resets its internal state to the initial state. */
    xml.clear();
  //  file->close();

}

void QCalParseEventDetailsXML::getEventInfo(QXmlStreamReader& xml)
{
	
	eventDetail event;
	while(!(xml.tokenType() == QXmlStreamReader::EndElement &&
					xml.name() == "entry")) 
	{
		if(xml.tokenType() == QXmlStreamReader::StartElement) 
			{
			/* We've found id */
			if(xml.name() == "title") 
				{
				//event.title=xml.text().toString();
				xml.readNext();
				if(xml.tokenType() == QXmlStreamReader::Characters) 
					{
					event.title=xml.text().toString();	
					
				    }
				//this->getTitle(xml);
				}
			if (xml.name() == "content")
				{
				xml.readNext();
				if(xml.tokenType() == QXmlStreamReader::Characters) 
					{
					event.content=xml.text().toString();	
				    }
				}
			if (xml.name() == "where")
				{
				QXmlStreamAttributes attributes = xml.attributes();
				
				event.location = attributes.value("valueString").toString();
				}
			if (xml.name() == "when")
				{
				QXmlStreamAttributes attributes = xml.attributes();
				
				event.endTime = attributes.value("endTime").toString();
				event.startTime = attributes.value("startTime").toString();
				eventDetailsArray.append(event);
				}

			}
		
		/* ...and next... */
		xml.readNext();
	}

}


QVector<eventDetail> QCalParseEventDetailsXML::geteventDetailsArray()
	{
	
	return eventDetailsArray;
	}
